//
//  PainterView.m
//  ChartRefactor
//
//  Created by CaoJun on 15/3/11.
//  Copyright (c) 2015年 CaoJun. All rights reserved.
//

#import "PainterView.h"

using namespace ytx;

@implementation PainterView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)paint:(ViewModelIndexPosition *)viewModel withPainter:(PainterBase *) painter;
{
    self.indexPosition = viewModel;
    self.painter = painter;
    self.data = viewModel->getData();

    [self setNeedsDisplay];
}
- (void)paint
{
    if ([self hasData]) {
        [self setNeedsDisplay];
    }
}

- (void)clear
{
    self.indexPosition = NULL;
    self.painter = NULL;
    [self setNeedsDisplay];
}

-(BOOL) hasData
{
    return self.indexPosition == NULL ? false : self.data != NULL && self.data->hasData() && self.painter != NULL;
}

- (void)drawRect:(CGRect)cgrect
{
    if (![self hasData]){
        return;
    }

    CGContextRef context = UIGraphicsGetCurrentContext();

    ChartCanvas * ctx = [ChartCanvas new];
    ctx.context = context;
    ctx.bounds = self.bounds;
    
    [self.painter drawRect:cgrect withContext:ctx withViewModelIndexPosition:self.indexPosition];
}

@end
